package com.ll.notice.service.impl;

import com.ll.notice.dao.ISmsContentMapper;
import com.ll.notice.entity.SmsNotice;
import com.ll.notice.service.ISmsNoticeService;
import com.webservice.client.dongguan.ESBService;
import com.webservice.client.dongguan.ESBServiceSoap;
import com.webservice.client.zhongshansanyuan.SendMessageCommRt;
import com.webservice.client.zhongshansanyuan.WebServerForComm;
import com.webservice.client.zhongshansanyuan.WebServerForCommSoap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SmsNoticeServiceImpl implements ISmsNoticeService {


    @Autowired
    ISmsContentMapper smsContentMapper;

    @Value("${hospital}")
    String hospital;

    @Override
    public void sendMessage(){

        if ("zhongshansanyuan".equals(hospital)) {

            List<SmsNotice> smsNotices = smsContentMapper.findSmsContentList();
            WebServerForComm webServerForComm = new WebServerForComm();
            WebServerForCommSoap webServerForCommSoap = webServerForComm.getWebServerForCommSoap();
            SendMessageCommRt sendMessageCommRt = new SendMessageCommRt();
            sendMessageCommRt.setUserName("thygxt");
            sendMessageCommRt.setPassword("thygxt");

            log.info("开始发送短信");
            smsNotices.stream().forEach(smsNotice -> {
                sendMessageCommRt.setDocPhone(smsNotice.getReceiverMobile());
                sendMessageCommRt.setMsgContent(smsNotice.getSmsContent());
                String s = webServerForCommSoap.sendMsgComm(sendMessageCommRt);
                log.info("手机号:" + smsNotice.getReceiverMobile() + "，短信内容:" + smsNotice.getSmsContent());
                smsContentMapper.updateSmsNotice(smsNotice.getSmsId());
            });
            log.info("开始发送短信结束");
        }
        if ("dongguan".equals(hospital)){
            String str = "<ESBEnvelope xmlns=\"http://ESB.TopSchemaV2\"><ESBHeader><HeaderControl AppCode=\"APP009\" Password=\"oZS1lPA4emEixPN3\" MessageCategory=\"VX_SendMessage\" Version=\"1\" CreateTime=\"请用当前时间\" /></ESBHeader><ESBBody><BodyControl CallType=\"\"/><BusinessRequest><![CDATA[<?xml version=\"1.0\" encoding=\"utf-8\"?><WeChatMessage xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><AppName>测试消息提醒应用名称</AppName><Items><Item><ReceiverCode>";
            String str1 = "</ReceiverCode><Message>";
            String str2 = "</Message><MessageType>text</MessageType></Item></Items></WeChatMessage>]]></BusinessRequest></ESBBody></ESBEnvelope>";
            List<SmsNotice> smsNotices = smsContentMapper.findSmsContentList();
            ESBService esbService = new ESBService();
            ESBServiceSoap esbServiceSoap = esbService.getESBServiceSoap();
            smsNotices.stream().forEach(smsNotice -> {

                String xmlMessage = str + smsNotice.getAttendingDoctorId() + str1 + smsNotice.getSmsContent()+str2;
                String s = esbServiceSoap.callESB(xmlMessage);
                System.out.println(s);
                smsContentMapper.updateSmsNotice(smsNotice.getSmsId());
            });
        }
    }
}
